package com.cw.service.impl;

import com.aliyun.dysmsapi20170525.Client;
import com.aliyun.dysmsapi20170525.models.SendSmsRequest;
import com.cw.service.MessageCodeService;
import org.apache.commons.lang3.RandomStringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import com.aliyun.teaopenapi.models.*;

@Service
public class MessageCodeServiceImpl implements MessageCodeService {

    @Override
    public String createCode(String s) {
        //List<String> args = java.util.Arrays.asList(args_);
        Client client = null;
        try {
            client = createClient("LTAI4GCbdwnjCGwQj9Z3tr3N", "27ih4pc1W1tqey9X4mmCy1JUotAX7L");
        } catch (Exception e) {
            e.printStackTrace();
        }
        String code = RandomStringUtils.randomNumeric(4);
        SendSmsRequest sendSmsRequest = new SendSmsRequest()
                .setPhoneNumbers(s)
                .setSignName("东科创想")
                .setTemplateCode("SMS_162522027")
                .setTemplateParam("{\"sms_code\":\"" + code + "\"}");
        // 复制代码运行请自行打印 API 的返回值
        try {
            client.sendSms(sendSmsRequest);
            return code;
        } catch (Exception e) {
            e.printStackTrace();
        }

        return null;
    }

    public Client createClient(String accessKeyId, String accessKeySecret) throws Exception {
        Config config = new Config()
                // 您的AccessKey ID
                .setAccessKeyId(accessKeyId)
                // 您的AccessKey Secret
                .setAccessKeySecret(accessKeySecret);
        // 访问的域名
        config.endpoint = "dysmsapi.aliyuncs.com";
        return new Client(config);
    }

}
